<template>
  <div class="tree-select">
    <div class="side">
      <div
        :class="[
          'side-item',
          selectedIndex === index ? 'selected-side_item' : ''
        ]"
        v-for="(item, index) in list"
        @click="selectedIndex = index"
      >
        {{ item.name }}
      </div>
    </div>
    <div class="content">
      <div
        :class="[
          'content-item',
          selectedId.startsWith(item.id) ? 'selected-content_item' : ''
        ]"
        v-for="item in selectedList"
        @click="confirm(item.id)"
      >
        <span>{{ item.name }}</span>
        <i
          class="el-icon-check"
          style="color:#EE0A24"
          v-show="item.id === selectedId"
        ></i>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    selectedId: {
      type: String,
      default: ""
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  computed: {
    selectedList() {
      return this.list.length ? this.list[this.selectedIndex].children : [];
    }
  },
  watch: {
    selectedId: {
      handler(id) {
        if (!id) return "";
        const index = this.list.findIndex(item =>
          item.children.find(item2 => item2.id === id)
        );
        if (index !== -1) this.selectedIndex = index;
      },
	  immediate:true
    }
  },
  data() {
    return {
      selectedIndex: 0
    };
  },
  methods: {
    confirm(id) {
      this.$emit("confirm", id);
    },
  }
};
</script>
<style lang="scss" scoped>
.tree-select {
  display: flex;
  box-sizing: border-box;
  background-color: #fff;
  height: 260px;
  .side {
    background-color: #f7f8fa;
    font-size: 14px;

    .side-item {
      padding: 15px 0 15px 10px;
      width: 100px;
      cursor: pointer;
    }
    .selected-side_item {
      background-color: white;
    }
  }
  .content {
    flex: 1;
    padding-right: 10px;
    overflow: auto;
    .content-item {
      padding: 4px 10px;
      display: flex;
      justify-content: space-between;
      height: 20px;
      cursor: pointer;
    }
    &::-webkit-scrollbar {
      width: 7px;
      height: 8px;
    }

    &::-webkit-scrollbar-track {
      background-color: rgb(0 0 0 / 5%);
    }

    &::-webkit-scrollbar-thumb {
      background-color: rgb(144 147 153 / 30%);
      border-radius: 2px;
      box-shadow: inset 0 0 6px rgb(0 0 0 / 20%);
    }

    &::-webkit-scrollbar-thumb:hover {
      background-color: black;
    }
    .selected-content_item {
      color: #ee0a24;
    }
  }
}
</style>
